package com.heima.behavior.service.impl;

import com.heima.behavior.model.dto.FollowBehaviorDto;
import com.heima.behavior.model.pojo.ApFollowBehavior;
import com.heima.behavior.service.ApFollowBehaviorService;
import com.heima.behavior.utils.AppThreadLocalUtil;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class ApFollowBehaviorServiceImpl implements ApFollowBehaviorService {

    @Resource
    private MongoTemplate mongoTemplate;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 关注行为
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult follow(FollowBehaviorDto dto) {
        //1.参数校验
        if (dto.getArticleId() == null || dto.getAuthorId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        Long userId = AppThreadLocalUtil.getId();
        if (userId == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        //2.判断用户是否已经关注
        String follow_hash_key = "USER_FOLLOW_" + userId;
        String fans_hash_key = "USER_FANS_" + dto.getAuthorId();
        Boolean hasKey = stringRedisTemplate.opsForHash().hasKey(follow_hash_key, dto.getAuthorId() + "");

        //2.1用户已关注--关注行为
        if (hasKey && dto.getOperation() == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS);
        }
        //2.2用户已关注--关取消注行为
        if (hasKey && dto.getOperation() == 1) {
            //先删除缓存中的数据
            stringRedisTemplate.opsForHash().delete(follow_hash_key, dto.getAuthorId() + "");
            stringRedisTemplate.opsForHash().delete(fans_hash_key, userId + "");
            //再删除mongodb中的数据
            Query query = Query.query(Criteria.where("userId").is(userId).and("followId").is(dto.getAuthorId()));
            mongoTemplate.remove(query, ApFollowBehavior.class);
            return ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS);
        }

        //2.3用户未关注 --关注行为
        if (!hasKey && dto.getOperation() == 0) {
            //保存数据到redis
            stringRedisTemplate.opsForHash().put(follow_hash_key, dto.getAuthorId() + "", "1");
            stringRedisTemplate.opsForHash().put(fans_hash_key, userId + "", "1");
            //保存数据到mongodb
            ApFollowBehavior apFollowBehavior = new ApFollowBehavior();
            apFollowBehavior.setArticleId(dto.getArticleId());
            apFollowBehavior.setCreatedTime(new Date());
            apFollowBehavior.setFollowId(dto.getAuthorId());
            apFollowBehavior.setUserId(userId);
            apFollowBehavior.setId(ObjectId.get().toHexString());
            mongoTemplate.insert(apFollowBehavior);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
